<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>MongoDB 备份和恢复 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>MongoDB 备份和恢复</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/MongoDB/">MongoDB</a>
			</span>
		
	</div>

	

	
		<h2 id="1-MongoDB-备份"><a href="#1-MongoDB-备份" class="headerlink" title="1. MongoDB 备份"></a>1. MongoDB 备份</h2><h3 id="备份指定库"><a href="#备份指定库" class="headerlink" title="备份指定库"></a>备份指定库</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mongodump -h ip -d dbname -o dir</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-h后面跟服务器ip，-d后面跟database名字，不加则备份所有库，-o后指定备份到哪里，它是一个目录</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%A4%87%E4%BB%BD%E5%92%8C%E6%81%A2%E5%A4%8D/01.png?raw=true" alt="01"><figcaption class="figure__caption">01</figcaption></figure></p>
<h3 id="备份所有库"><a href="#备份所有库" class="headerlink" title="备份所有库"></a>备份所有库</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mongodump -h ip -o dir</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%A4%87%E4%BB%BD%E5%92%8C%E6%81%A2%E5%A4%8D/02.png?raw=true" alt="02"><figcaption class="figure__caption">02</figcaption></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%A4%87%E4%BB%BD%E5%92%8C%E6%81%A2%E5%A4%8D/03.png?raw=true" alt="03"><figcaption class="figure__caption">03</figcaption></figure></p>
<h3 id="备份指定集合"><a href="#备份指定集合" class="headerlink" title="备份指定集合"></a>备份指定集合</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mongodump -d mydb -c testc -o /tmp/testc</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-c指定集合名字</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%A4%87%E4%BB%BD%E5%92%8C%E6%81%A2%E5%A4%8D/04.png?raw=true" alt="04"><figcaption class="figure__caption">04</figcaption></figure></p>
<h3 id="导出集合为json文件"><a href="#导出集合为json文件" class="headerlink" title="导出集合为json文件"></a>导出集合为json文件</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mongoexport -d mydb -c testc -o /tmp/testc.json</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-o后面跟的是一个文件名字</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%A4%87%E4%BB%BD%E5%92%8C%E6%81%A2%E5%A4%8D/05.png?raw=true" alt="05"><figcaption class="figure__caption">05</figcaption></figure></p>
<h2 id="2-MongoDB-恢复"><a href="#2-MongoDB-恢复" class="headerlink" title="2.MongoDB 恢复"></a>2.MongoDB 恢复</h2><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%A4%87%E4%BB%BD%E5%92%8C%E6%81%A2%E5%A4%8D/06.png?raw=true" alt="06"><figcaption class="figure__caption">06</figcaption></figure></p>
<h3 id="恢复所有库"><a href="#恢复所有库" class="headerlink" title="恢复所有库"></a>恢复所有库</h3><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%A4%87%E4%BB%BD%E5%92%8C%E6%81%A2%E5%A4%8D/07.png?raw=true" alt="07"><figcaption class="figure__caption">07</figcaption></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mongorestore --drop dir/</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中dir是备份所有库的目录名字，其中–drop可选，意思是当恢复之前先把之前的数据删除，不建议使用</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%A4%87%E4%BB%BD%E5%92%8C%E6%81%A2%E5%A4%8D/08.png?raw=true" alt="08"><figcaption class="figure__caption">08</figcaption></figure></p>
<h3 id="恢复指定库"><a href="#恢复指定库" class="headerlink" title="恢复指定库"></a>恢复指定库</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mongorestore -d mydb dir/</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-d跟要恢复的库名字，dir就是该库备份时所在的目录</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%A4%87%E4%BB%BD%E5%92%8C%E6%81%A2%E5%A4%8D/09.png?raw=true" alt="09"><figcaption class="figure__caption">09</figcaption></figure></p>
<h3 id="恢复集合"><a href="#恢复集合" class="headerlink" title="恢复集合"></a>恢复集合</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mongorestore --drop -d mydb -c mycol /tmp/234/mydb/mycol.bson</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-c后面跟要恢复的集合名字，dir是备份mydb库时生成文件所在路径，这里是一个bson文件的路径</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%A4%87%E4%BB%BD%E5%92%8C%E6%81%A2%E5%A4%8D/10.png?raw=true" alt="10"><figcaption class="figure__caption">10</figcaption></figure></p>
<h3 id="导入集合"><a href="#导入集合" class="headerlink" title="导入集合"></a>导入集合</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mongoimport -d mydb -c mycol --file /tmp/12345/mycol.json</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/MongoDB%20%E5%A4%87%E4%BB%BD%E5%92%8C%E6%81%A2%E5%A4%8D/11.png?raw=true" alt="11"><figcaption class="figure__caption">11</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里加 –drop 是因为之前恢复库和集合，所以加 –drop 强制覆盖。</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/MongoDB/5. MongoDB 备份和恢复/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
